package team.kirohuji.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import team.kirohuji.dao.SystemUserRoleDao;
import team.kirohuji.domain.SystemUser;
import team.kirohuji.domain.SystemUserRole;
import team.kirohuji.service.SystemUserRoleService;

/**
 * 系统用户角色实现类
 * 
 * @author kirohuji
 *
 */
@Service("systemUserRoleService")
public class SystemUserRoleServiceImpl implements SystemUserRoleService {

	/**
	 * @see SystemUserRoleDao systemUserRoleDao
	 */
	@Resource
	private SystemUserRoleDao systemUserRoleDao;

	/*
	 * 存入一条记录如果存在则更新 (non-Javadoc)
	 * 
	 * @see
	 * team.kirohuji.service.SystemUserRoleService#saveOrUpdate(java.lang.Integer,
	 * java.util.List)
	 * 
	 * @param userId 用户主键
	 * 
	 * @param roleIdList 角色主键列表
	 */
	@Override
	public void saveOrUpdate(Integer userId, List<Integer> roleIdList) {
		deleteBySystemUser(userId);
		SystemUser su=new SystemUser();
		su.setId(userId);
		su.setRoleIdList(roleIdList);
		systemUserRoleDao.saveBatch(su);
	}

	/*
	 * 查询角色列表 (non-Javadoc)
	 * 
	 * @see team.kirohuji.service.SystemUserRoleService#queryRoleList()
	 * 
	 * @return SystemUserRole列表
	 */
	@Override
	public List<SystemUserRole> queryRoleList() {
		return null;
	}

	/*
	 * 查询用户角色列表 (non-Javadoc)
	 * 
	 * @see team.kirohuji.service.SystemUserRoleService#queryRoleIdList(java.lang.
	 * Integer)
	 * 
	 * @param userId 用户主键
	 * 
	 * @return Integer列表
	 */
	@Override
	public List<Integer> queryRoleIdList(Integer userId) {
		return null;
	}

	/*
	 * 删除一条记录 (non-Javadoc)
	 * 
	 * @see team.kirohuji.service.SystemUserRoleService#delete(java.lang.Integer)
	 * 
	 * @param userId 用户主键
	 */
	@Override
	public void delete(Integer userId) {

	}

	@Override
	public void modifyManagerRole(Integer userid, String[] roleId, String[] systemRoles) {
		List<Integer> allRoles = systemUserRoleDao.queryRoleIdList(userid);

		// 判断提交的角色修改，如果取消原选项，则删除数据库中的相应数据，如果是新选项，则插入新数据
		for (int i = 0; i < systemRoles.length; i++) {
			if (allRoles.contains(Integer.parseInt(roleId[i])) == true && "false".equals(systemRoles[i]))
				systemUserRoleDao.deleteSystemUserRole(userid, Integer.parseInt(roleId[i]));
			else if (allRoles.contains(Integer.parseInt(roleId[i])) == false && "true".equals(systemRoles[i]))
				systemUserRoleDao.insertSystemUserRole(userid, Integer.parseInt(roleId[i]));
		}
		/*
		 * if(roles.contains(1) == true && "false".equals(systemManager))
		 * systemUserRoleDao.deleteSystemUserRole(userid, 1); else if(roles.contains(1)
		 * == false && "true".equals(systemManager))
		 * systemUserRoleDao.insertSystemUserRole(userid, 1);
		 * 
		 * if(roles.contains(2) == true && "false".equals(securityOfficer))
		 * systemUserRoleDao.deleteSystemUserRole(userid, 2); else if(roles.contains(2)
		 * == false && "true".equals(securityOfficer))
		 * systemUserRoleDao.insertSystemUserRole(userid, 2);
		 * 
		 * if(roles.contains(3) == true && "false".equals(auditor))
		 * systemUserRoleDao.deleteSystemUserRole(userid, 3); else if(roles.contains(3)
		 * == false && "true".equals(auditor))
		 * systemUserRoleDao.insertSystemUserRole(userid, 3);
		 * 
		 * if(roles.contains(4) == true && "false".equals(normalManager))
		 * systemUserRoleDao.deleteSystemUserRole(userid, 4); else if(roles.contains(4)
		 * == false && "true".equals(normalManager))
		 * systemUserRoleDao.insertSystemUserRole(userid, 4);
		 */
	}

	@Override
	public int deleteBySystemUser(Integer userid) {
		return systemUserRoleDao.deleteBySystemUser(userid);
	}

}
